<?php

// @formatter:off
/**
 * @file search.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-05-04 01:33:52 +0000 (Sun, 04 May 2014) $
 *
 * $Id: search.php 111 2014-05-04 01:33:52Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/frontview/actions/content/search.php $
 */
// @formatter:on

class ActionControllerContentSearch extends WCActionController {
	//
	// Public methods.
	public function check() {
		$check = parent::check();

		if($check == WC_HTTPERROR_OK) {
			global $wcProfile;

			if(!$wcProfile->allowedTo(WC_PERM_SPECIAL_SEARCH)) {
				global $errno;
				global $error;

				$check = WC_HTTPERROR_FORBIDDEN;
				$errno = $check;
				$error = "You're not allowed to search";
			}
		}

		return $check;
	}
	//
	// Protected methods.
	protected function subRun() {
		$ok = true;

		//
		// Loading global pointers.
		global $wcGroup;
		global $wcDefaults;
		global $wcSkinConfig;
		//
		// Setting section title.
		$this->_manager->PATH = "Search";
		//
		// Loading current page information
		// @{
		$page = isset($_REQUEST["page"]) ? $_REQUEST["page"] : 1;
		$maxPerPage = isset($wcSkinConfig->max_per_page->search) ? $wcSkinConfig->max_per_page->search : 25;
		$minSrchInPage = $maxPerPage * ($page - 1);
		$maxSrchInPage = $minSrchInPage + $maxPerPage;
		// @}

		if(isset($_REQUEST["pattern"])) {
			$timer = WCTimer::Instance();
			$timer->start(__FILE__);

			$pattern = "";
			$exceptions = false;
			if(isset($_REQUEST["pattern"])) {
				$pattern = turldecode($_REQUEST["pattern"]);
				$pattern = implode(" ", array_filter(explode(" ", $pattern)));
				$this->_manager->search_pattern = $pattern;
				$this->_manager->search_words = WCSearch::StringToPatterns($pattern);
			} if(isset($_REQUEST["exceptions"])) {
				$exceptions = turldecode($_REQUEST["exceptions"]);
				$exceptions = implode(" ", array_filter(explode(" ", $exceptions)));
				$this->_manager->search_exceptions = $exceptions;
			}
			//
			// Completing section title.
			$this->_manager->PATH .= ": {$this->_manager->search_pattern}";
			//
			// Loading type filt ers.
			$onlyTypes = array();
			foreach(WCSearchFactory::Types() as $type) {
				$param = "O-{$type}";
				if(isset($_REQUEST[$param])) {
					$this->_manager->{$param} = true;
					$onlyTypes[] = $type;
				}
			}
			//
			// Searching
			$result = WCSearch::I()->search($pattern, $exceptions, $onlyTypes);
			//
			// Starting a time to know how much it takes to transform found
			// item into printable items.
			$timer->start(__FILE__."[build]");
			//
			// Filtering groups.
			// @{
			$gid = $wcGroup->id();
			foreach($result as $key => $item) {
				if($item->groupId() != $gid) {
					unset($result[$key]);
				}
			}
			// @}
			//
			// Counts.
			$result_count = count($result);
			$this->_manager->result_count = 0;
			$search_items = array();
			//
			// Building printable items, if there is any.
			if($result_count) {
				$count = 0;
				$ids = 1;
				foreach($result as $item) {
					if($count >= $minSrchInPage && $count < $maxSrchInPage) {
						$objItem = WCSearchFactory::ItemBuilder($item);
						if($objItem) {
							$aux = array();
							$aux["name"] = $objItem->name();
							#$aux["full-name" = $item->fullName();
							$aux["factor"] = $item->score();
							$aux["score"] = (string)$item->score();
							$aux["hits"] = 0;
							$aux["link"] = "";
							$aux["type"] = "";
							$aux["id"] = $ids++;

							switch($item->type()) {
								case "CX":
									$aux["link"] = "?group={$gid}&action=".WC_ACTION_COMIC_SHOW."&comic={$item->id()}";
									$aux["type"] = "Comic";
									break;
								case "IM":
									$aux["link"] = "?group={$gid}&action=".WC_ACTION_IMAGE_SHOW."&image={$item->id()}";
									$aux["type"] = "Image";
									break;
								case "TA":
									$aux["link"] = "?group={$gid}&action=".WC_ACTION_TAG_SHOW."&tag={$item->id()}";
									$aux["type"] = "Tag";
									break;
								case "DI":
									$aux["link"] = "?group={$gid}&action=".WC_ACTION_DIRECTORY_SHOW."&directory={$item->id()}";
									$aux["type"] = "Directory";
									break;
								case "RL":
									$aux["link"] = "?group={$gid}&action=".WC_ACTION_RULE_SHOW."&rule={$item->id()}";
									$aux["type"] = "Rule";
									break;
								case "CL":
									$aux["link"] = "?group={$gid}&action=".WC_ACTION_COLLECTION_SHOW."&collection={$item->id()}";
									$aux["type"] = "Collection";
									break;
							}

							if($objItem instanceof WCPreviews) {
								if($objItem->hasPreviews()) {
									$aux["preview"] = $objItem->preview();
									$aux["previews"] = $objItem->previews();
								} elseif($objItem->hasPreview()) {
									$aux["preview"] = $objItem->preview();
									$aux["previews"] = array(
										$aux["preview"]
									);
								} else {
									$aux["preview"] = $wcDefaults["preview"];
								}
							}

							$subItemsCount = 0;
							$haveSubItems = false;

							if($objItem instanceof WCCountComics || $objItem instanceof WCCountImages || $objItem instanceof WCCountDirectories) {
								$subItemsCount = $objItem->countItems();
								$haveSubItems = true;
							}

							if($haveSubItems) {
								$aux["count"] = $subItemsCount;
							}

							if(!$haveSubItems || $subItemsCount > 0) {
								$search_items[] = $aux;
							} else {
								$count--;
							}
						}
					}
					$count++;
				}
				$this->_manager->search_items = $search_items;
				$this->_manager->result_count = $count;
			}
			$timer->stop(__FILE__."[build]");


			$timer->stop(__FILE__);

			$times = array();
			$times[] = array(
				"what" => "label_elapsed",
				"time" => $timer->timer(__FILE__)
			);
			$times[] = array(
				"what" => "label_search",
				"time" => WCSearch::I()->elapsedTime()
			);
			$times[] = array(
				"what" => "label_build",
				"time" => $timer->timer(__FILE__."[build]")
			);

			if($exceptions) {
				$times[] = array(
					"what" => "label_exceptions",
					"time" => WCSearch::I()->timer("exceptions")
				);
				$times[] = array(
					"what" => "label_filtering",
					"time" => WCSearch::I()->timer("filtering")
				);
			}
			$this->_manager->times = $times;

			WCFVPager::GetAssignsIntoManager(ceil($this->_manager->result_count / $maxPerPage));
		}

		return $ok;
	}
}
